package com.scau.classOnline.utils.time;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Create by ji on 2019-07-09
 */
public class TimeFormatUtil {

    /**
     * 时间转换
     * date -> String
     * @param date
     * @param pattern 转换获得的数据格式，例如yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String convertDateToString(Date date,TimePattern pattern) {
        DateTimeFormatter dfs = DateTimeFormatter.ofPattern(pattern.getPattern());
        return dfs.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()));
    }

    public static Date convertStringToDate(String dateTime,TimePattern pattern){
        DateTimeFormatter dfs = DateTimeFormatter.ofPattern(pattern.getPattern());
        Instant instant = LocalDate.parse(dateTime, dfs)
                .atStartOfDay()
                .atZone(ZoneId.systemDefault())
                .toInstant();
        return Date.from(instant);
    }

}
